using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace HyperbolaControl
{
    public class HyperbolaControl : Control
    {
        private Timer timer;
        private float t = 0;
        public List<float> Curve1Values { get; } = new List<float>();
        public List<float> Curve2Values { get; } = new List<float>();
        public float XAxisMin { get; set; } = 0;
        public float XAxisMax { get; set; } = 100;
        public int MaxDataPoints { get; set; } = 100;
        
        public void UpdateData(float[] data1, float[] data2)
        {
            Curve1Values.Clear();
            Curve2Values.Clear();
            
            if (data1 != null)
                Curve1Values.AddRange(data1);
                
            if (data2 != null)
                Curve2Values.AddRange(data2);
                
            this.Invalidate();
        }

        public HyperbolaControl()
        {
            this.DoubleBuffered = true;

            timer = new Timer();
            timer.Interval = 50;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            // 限制数据点数量
            if (Curve1Values.Count > MaxDataPoints)
            {
                Curve1Values.RemoveAt(0);
                Curve2Values.RemoveAt(0);
            }

            t += 0.1f;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 绘制黑色背景
            g.Clear(Color.Black);

            // 绘制坐标轴
            Pen axisPen = new Pen(Color.Gray, 1);
            //g.DrawLine(axisPen, 0, this.Height/2, this.Width, this.Height/2);
            //g.DrawLine(axisPen, this.Width/2, 0, this.Width/2, this.Height);

            // 绘制水平刻度线(10等分)
            for (int i = 1; i < 10; i++)
            {
                float y = this.Height * i / 10;
                g.DrawLine(axisPen, 0, y, this.Width, y);
            }

            // 绘制垂直刻度线(4等分)
            for (int i = 1; i < 4; i++)
            {
                float x = this.Width * i / 4;
                g.DrawLine(axisPen, x, 0, x, this.Height);
            }

            // 绘制第一条曲线(绿色)
            if (Curve1Values.Count > 1)
            {
                Pen curve1Pen = new Pen(Color.Lime, 2);
                PointF[] points1 = new PointF[Curve1Values.Count];

                for (int i = 0; i < Curve1Values.Count; i++)
                {
                    float y = this.Height - ((Curve1Values.Count - i) * (this.Height / MaxDataPoints));
                    float x = this.Width * (Curve1Values[i] - XAxisMin) / (XAxisMax - XAxisMin);
                    points1[i] = new PointF(x, y);
                }
                g.DrawCurve(curve1Pen, points1);
            }

            // 绘制第二条曲线(蓝色)
            if (Curve2Values.Count > 1)
            {
                Pen curve2Pen = new Pen(Color.Cyan, 2);
                PointF[] points2 = new PointF[Curve2Values.Count];

                for (int i = 0; i < Curve2Values.Count; i++)
                {
                    float y = this.Height - ((Curve2Values.Count - i) * (this.Height / MaxDataPoints));
                    float x = this.Width * (Curve2Values[i] - XAxisMin) / (XAxisMax - XAxisMin);
                    points2[i] = new PointF(x, y);
                }
                g.DrawCurve(curve2Pen, points2);
            }
        }
    }
}